﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Manager;
using StarryEdge.Core.Models;
using StarryEdge.Core.Models.Interact;
using StarryEdge.Core.Parameter;
using StarryEdge.Menu.SettingSystem.ViewModels.Dialog;
using StarryEdge.Menu.SettingSystem.Views.Dialog;
using StarryEdge.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Menu.SettingSystem.ViewModels
{
    public class LowerComputerInteractionViewModel
    {
        public SystemConfigParameter SystemConfig { get; set; }
        private IObjectManager _objectManager;
        private IDeviceInfoManage _infoManage;
        public LowerComputerInteractionViewModel(ISystemConfigParameter systemConfig, IObjectManager objectManager, IDeviceInfoManage infoManage)
        {
            _infoManage = infoManage;
            SystemConfig = (SystemConfigParameter)systemConfig;
            _objectManager = objectManager;
        }

        public DelegateCommand New => new DelegateCommand(() =>
        {
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) DialogHost.Close(PopUpTypes.Global.ToString());
            DialogHost.Show(new NewPLCResection()
            {
                DataContext = new NewPLCResectionPopupDialog(SystemConfig, default, SystemConfig.SystemConfig.IsCip)
            }, PopUpTypes.Global.ToString());
        });

        public DelegateCommand<PLCResectionModel> PLCResectionMouseDoubleClick => new DelegateCommand<PLCResectionModel>(sender =>
        {
            if (sender != null)
            {
                _ = DialogHost.Show(new NewPLCResection() { DataContext = new NewPLCResectionPopupDialog(SystemConfig, sender, SystemConfig.SystemConfig.IsCip, true) }, PopUpTypes.Global.ToString());
            }
        });

        #region 服务
        public DelegateCommand SignalAddClick => new DelegateCommand(() => 
        {
            if (SystemConfig.SystemConfig.IsCip)
            {
                DialogHost.Show(new NewScanSignal() { DataContext = new NewScanSignalPopupDialog<string>(SystemConfig.StringSignalList, SystemConfig, default) }, PopUpTypes.Global.ToString());
            }
            else
            {
                DialogHost.Show(new NewScanSignal() { DataContext = new NewScanSignalPopupDialog<int>(SystemConfig.AddressSignalList, SystemConfig, default) }, PopUpTypes.Global.ToString());
            }
        });

        public DelegateCommand<object> SignalMouseDoubleClick => new DelegateCommand<object>(sender =>
        {
            if (sender != null)
            {
                if (sender is ScanSignalModel<string> model)
                {
                    DialogHost.Show(new NewScanSignal() { DataContext = new NewScanSignalPopupDialog<string>(SystemConfig.StringSignalList, SystemConfig, model, true) }, PopUpTypes.Global.ToString());
                }
                if (sender is ScanSignalModel<int> modelint)
                {
                    DialogHost.Show(new NewScanSignal() { DataContext = new NewScanSignalPopupDialog<int>(SystemConfig.AddressSignalList, SystemConfig, modelint, true) }, PopUpTypes.Global.ToString());
                }
            }
        });
        #endregion

        #region 任务
        public DelegateCommand InteractAddClick => new DelegateCommand(() => {
            if (SystemConfig.SystemConfig.IsCip)
            {
                DialogHost.Show(new NewInteractAddress() { DataContext = new NewInteractAddressPopupDialog<string>(SystemConfig.StringIAddressList,
                    SystemConfig.StringSignalList,
                    SystemConfig, _objectManager, default, _infoManage) }, 
                    PopUpTypes.Global.ToString());
            }
            else
            {
                DialogHost.Show(new NewInteractAddress()
                {
                    DataContext = new NewInteractAddressPopupDialog<int>(SystemConfig.AddressIAddressList,
                    SystemConfig.AddressSignalList,
                    SystemConfig, _objectManager, default, _infoManage)
                },
                    PopUpTypes.Global.ToString());
            }
        });

        public DelegateCommand<object> InteractMouseDoubleClick => new DelegateCommand<object>(sender =>
        {
            if (sender != null)
            {
                if (sender is InteractAddressModel<string> model)
                {
                    DialogHost.Show(new NewInteractAddress()
                    {
                        DataContext = new NewInteractAddressPopupDialog<string>(SystemConfig.StringIAddressList,
                        SystemConfig.StringSignalList,
                        SystemConfig, _objectManager, model, _infoManage, true)
                    },
                        PopUpTypes.Global.ToString());
                }
                if (sender is InteractAddressModel<int> modelint)
                {
                    DialogHost.Show(new NewInteractAddress()
                    {
                        DataContext = new NewInteractAddressPopupDialog<int>(SystemConfig.AddressIAddressList,
                        SystemConfig.AddressSignalList,
                        SystemConfig, _objectManager, modelint, _infoManage, true)
                    },
                        PopUpTypes.Global.ToString());
                }
            }
        });
        #endregion

    }
}
